package com.ruoyi.websocket.handler;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public interface IMessageProcessor {
    static final Logger log = LoggerFactory.getLogger(IMessageProcessor.class);
    Map<String, ChannelHandlerContext> allChannel = new ConcurrentHashMap<>();
    void handlerAdded(ChannelHandlerContext ctx);

    default void process(ChannelHandlerContext ctx, TextWebSocketFrame frame){
        log.info("未实现process方法,text:{}",frame.text());
    }

    default void beforeProcess(ChannelHandlerContext ctx, TextWebSocketFrame frame){
    }
    default void afterProcess(ChannelHandlerContext ctx, TextWebSocketFrame frame){
    }

    default void processJson(JSONObject json, ChannelHandlerContext ctx, TextWebSocketFrame frame){
        log.info("未实现processJson方法,text:{}",frame.text());
    }
    default void processArray(JSONArray json, ChannelHandlerContext ctx, TextWebSocketFrame frame){
        log.info("未实现processArray方法,text:{}",frame.text());
    }

    void handlerRemoved(ChannelHandlerContext ctx);
    public default void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception{
        log.error("未实现exceptionCaught方法,cause:{}",cause);

    }
}
